﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Modles.DTO;
using Modles.Models;

namespace BilbilDAL
{
    public class VideoForwardDAL
    {
        /// <summary>
        /// 通过前端传过来的用户信息表id和视频信息表id给视频转发表添加数据
        /// </summary>
        /// <param name="mod_DTo"></param>
        /// <returns></returns>
        public bool AddVideoForward(VideoForward_DTO mod_DTO)
        {
            try
            {
                VideoForward videoForward = new VideoForward()
                {
                    UserInfoId = mod_DTO.UserInfoId,
                    VideoInfoId = mod_DTO.VideoInfoId
                };
                using (VideoPlatformDbContext con =new VideoPlatformDbContext() )
                {
                    var userExists = con.UserInfos.Any(x => x.Id == mod_DTO.UserInfoId);
                    if (!userExists)
                    {
                        return false;
                       
                    }
                    var videoExists = con.VideoInfos.Any(x => x.Id == mod_DTO.VideoInfoId);
                    if (!videoExists)
                    {
                        return false;

                    }
                    con.VideoForwards.Add(videoForward);
                    int count = con.SaveChanges();
                    if (count > 0)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        /// <summary>
        /// 获取视频转发量
        /// </summary>
        /// <param name="videoInfoId"></param>
        /// <returns></returns>
        public int GetVideoForwardCount(int videoInfoId)
        {
            using (VideoPlatformDbContext con = new VideoPlatformDbContext())
            {
                return con.VideoForwards.Where(x => x.VideoInfoId == videoInfoId).Count();
            }
        }
    }
}
